﻿using FPSO.Models.TagMapping;
using Microsoft.Practices.Prism.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;

namespace FPSO.Models.ViewModel.Controls
{
    public class ValveViewModel : NotificationObject
    {
        public ValveViewModel(string valveId)
        {
            ValveId = valveId;
			openCmd = false;
			closeCmd = false;
			statusOpened = false;
			statusClosed = false;
			statusFault = false;

			openRatio = 0;
			//active = true;
			ActiveStatus = 0;
			//stateColor = Color.FromRgb(0x33, 0xA1, 0xC9);
		}

		public string ValveId { get; private set; }

		//valve active对应状态序列
		//statusFault = 1, fault, activeStatus = 0;
		//statusOpened = 1, opened, activeStatus = 1;
		//openCmd = 1, Opening, activeStatus = 2;
		//statusClosed = 1, closed, activeStatus = 3;
		//closeCmd = 1, closing, activeStatus = 4;

		private bool statusFault;
		[FieldMapper("STATUSFAULT", TypeCode.Int32, 1)]
		public bool StatusFault
		{
			get { return statusFault; }
			set
			{
				if (value == true)
				{
					//active = false;
					ActiveStatus = 0;
					//RaisePropertyChanged("Active");
					RaisePropertyChanged("ActiveStatus");
				}
			}
		}

		private bool statusOpened;
		[FieldMapper("STATUSOPENED", TypeCode.Int32, 1)]
		public bool StatusOpened
		{
			get { return statusOpened; }
			set
			{
				statusOpened = value;
				if (value == true)
				{
					//active = true;
					ActiveStatus = 1;
					//RaisePropertyChanged("Active");
					RaisePropertyChanged("ActiveStatus");
				}
			}
		}

		private bool openCmd;
		[FieldMapper("OPENCMD", TypeCode.Int32, 1)]
		public bool OpenCmd
		{
			get { return openCmd; }
			set
			{
				if(value == true)
				{
					//active = true;
					ActiveStatus = 2;
					//RaisePropertyChanged("Active");
					RaisePropertyChanged("ActiveStatus");
				}
			}
		}

		private bool statusClosed;
		[FieldMapper("STATUSCLOSED", TypeCode.Int32, 1)]
		public bool StatusClosed
		{
			get { return statusClosed; }
			set
			{
				statusClosed = value;
				if (value == true)
				{
					//active = false;
					ActiveStatus = 3;
					//RaisePropertyChanged("Active");
					RaisePropertyChanged("ActiveStatus");
				}
			}
		}

		private bool closeCmd;
		[FieldMapper("CLOSECMD", TypeCode.Int32, 1)]
		public bool CloseCmd
		{
			get { return closeCmd; }
			set
			{
				if(value == true)
				{
					//active = false;
					ActiveStatus = 4;
					//RaisePropertyChanged("Active");
					RaisePropertyChanged("ActiveStatus");
				}
			}
		}

		//以下属性应该不需要
		private int openRatio;
		[FieldMapper("OPENRATIO", TypeCode.Int32, 1)]
		public int OpenRatio
		{
			get { return openRatio; }
			set
			{
				openRatio = value;
				RaisePropertyChanged("OpenRatio");
				if(openRatio < 1.0)
                    ActiveStatus = 3;
				else if(openRatio > 99.0)
                    ActiveStatus = 1;
				else if(openRatio >= 1.0 && openRatio <= 99.0)
                    ActiveStatus = 2;
				else
                    ActiveStatus = 0;
            }
        }

		//private bool active;
		//[FieldMapper("ACTIVE", TypeCode.Int32, 1)]
		//public bool Active
		//{
		//	get { return active; }
		//	set
		//	{
		//		active = value;
		//		RaisePropertyChanged("Active");
		//	}
		//}

		private int activeStatus;
		//[FieldMapper("ACTIVE", TypeCode.Int32, 1)]
		public int ActiveStatus
		{
			get { return activeStatus; }
			set
			{
				activeStatus = value;
				RaisePropertyChanged("ActiveStatus");
			}
		}

		//private Color stateColor;
		//public Color StateColor
		//{
		//	get { return stateColor; }
		//	set
		//	{
		//		stateColor = value;
		//		RaisePropertyChanged("StateColor");
		//	}
		//}
	}
}
